package com.ang.juc.n4.t1;

import lombok.extern.slf4j.Slf4j;

/**
 * wait,notify+Boolean变量
 * 实现t2先运行，t1后运行
 */
@Slf4j
public class Test1 {
    //锁对象
    static Object obj = new Object();
    //标识，t2是否运行
    static boolean t2IsRunning = false;

    public static void main(String[] args) {

        //2个线程
        Thread t1 = new Thread(() -> {
            synchronized (obj){
                while (!t2IsRunning){
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

                log.info("t1运行");
            }
        });

        Thread t2 = new Thread(() -> {
            synchronized (obj){
                log.info("t2运行");

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                t2IsRunning = true;
                //唤醒t1
                obj.notify();
            }
        });

        t1.start();
        t2.start();
    }
}
